<?php
/* SVN FILE: $Id: marketCache.php 228 2009-09-25 15:24:18Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket Cache functions
* ibMarket IPB cache functions
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 228 $
* @lastrevision $Date: 2009-09-25 15:24:18 +0000 (Fri, 25 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-25 15:24:18 +0000 (Fri, 25 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/sources/marketCache.php $
*/

final class marketCache
{
	/**#@+
	 * Registry Object Shortcuts
	 *
	 * @access	protected
	 * @var		object
	 */
	protected $registry;
	protected $DB;
	protected $settings;
	protected $request;
	protected $lang;
	protected $member;
	protected $memberData;
	protected $cache;
	protected $caches;
	/**#@-*/
	
	/**
	 * Make the registry shortcuts
	 *
	 * @access	public
	 * @param	object	ipsRegistry reference
	 * @return	void
	 */
	public function makeRegistryShortcuts( ipsRegistry $registry )
	{
		/* Make registry objects */
		$this->registry   =  $registry;
		$this->DB         =  $this->registry->DB();
		$this->settings   =& $this->registry->fetchSettings();
		$this->request    =& $this->registry->fetchRequest();
		$this->lang       =  $this->registry->getClass('class_localization');
		$this->member     =  $this->registry->member();
		$this->memberData =& $this->registry->member()->fetchMemberData();
		$this->cache      =  $this->registry->cache();
		$this->caches     =& $this->registry->cache()->fetchCaches();
	}
	
	/**
	 * Rebuilds categories cache
	 *
	 * @access	public
	 * @return	void
	 */
	public function cacheCategories()
	{
		$cache = array();
		
		$this->DB->build(array('select'=>'*','from'=>'market_categories','order'=>'cat_id ASC'));
		$q = $this->DB->execute();
		while($category = $this->DB->fetch($q))
		{
			$cache[$category['cat_id']] = $category;
		}
		
		$this->cache->setCache( 'market_category', $cache, array( 'array' => 1, 'deletefirst' => 1, 'donow' => 0 ) );
	}
	
	/**
	 * Rebuilds items cache
	 *
	 * @access	public
	 * @return	void
	 */
	public function cacheItems()
	{
		$cache = array();
		
		$this->DB->build(array('select'=>'*','from'=>'market_items'));
		$q = $this->DB->execute();
		while($item = $this->DB->fetch($q))
		{
			$item['limit_buy'] = unserialize($item['limit_buy']);
			$item['limit_use'] = unserialize($item['limit_use']);
			$cache[$item['item_id']] = $item;
		}
		
		$this->cache->setCache( 'market_items', $cache, array( 'array' => 1, 'deletefirst' => 1, 'donow' => 0 ) );
	}
	
	/**
	 * Rebuilds plugins cache
	 *
	 * @access	public
	 * @return	void
	 */
	public function cachePlugins()
	{
		$cache = array();
		
		$this->DB->build(array('select'=>'*','from'=>'market_plugins','order'=>'position ASC'));
		$q = $this->DB->execute();
		while($plugin = $this->DB->fetch($q))
		{
			$cache[$plugin['directory']] = $plugin;
		}
		
		$this->cache->setCache( 'market_plugin', $cache, array( 'array' => 1, 'deletefirst' => 1, 'donow' => 0 ) );
	}
	
	/**
	 * Rebuilds wages cache
	 *
	 * @access	public
	 * @return	void
	 */
	public function cacheWages()
	{
		$cache = array();
		$cache['id'] = array();
		
		$this->DB->build(array('select'=>'*','from'=>'market_wages'));
		$q = $this->DB->execute();
		while($wage = $this->DB->fetch($q))
		{
			$cache['id'][$wage['w_id']] = $wage;
		}
		
		$this->cache->setCache( 'market_wages', $cache, array( 'array' => 1, 'deletefirst' => 1, 'donow' => 0 ) );
	}
	
	/**
	 * Rebuilds taxes cache
	 *
	 * @access	public
	 * @return	void
	 */
	public function cacheTaxes()
	{
		$cache = array();
		$cache['id'] = array();
		
		$this->DB->build(array('select'=>'*','from'=>'market_taxes'));
		$q = $this->DB->execute();
		while($tax = $this->DB->fetch($q))
		{
			$tax['interval'] = max(60 * $tax['t_minute'] + 3600 * $tax['t_hour'] + 86400 * $tax['t_day'] + 604800 * $tax['t_week'],1);
			$cache['id'][$tax['t_id']] = $tax;
		}
		
		$this->cache->setCache( 'market_taxes', $cache, array( 'array' => 1, 'deletefirst' => 1, 'donow' => 0 ) );
	}
}
?>